package com.managementSystem.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.managementSystem.client.RegisterBeanServiceAsync;
import com.managementSystem.client.StaffBeanServiceAsync;
import com.managementSystem.client.event.AuthenticateStaff;
import com.managementSystem.client.event.RegisterStaff;
import com.managementSystem.client.presenter.RegisterPresenter.Display;
import com.managementSystem.shared.RegisterBean;
import com.managementSystem.shared.StaffBean;

public class RegisterPresenter implements Presenter {
	RegisterBean registeredStaff =null;
	public interface Display {
		HasClickHandlers getSubmitButton();
		HasValue<String> getID();
		HasValue<String> getUserName();
		HasValue<String> getFirstName();
		HasValue<String> getLastName();
		HasValue<String> getPassword();
		HasValue<String> getEmail();
		HasValue<String> getPnum();
		HasValue<String> getSnum();
		HasValue<String> getType();
		Widget asWidget();
	}
	private final StaffBeanServiceAsync rpcStaffBeanService;
	private final RegisterBeanServiceAsync rpcRegisterBeanService;
	private final HandlerManager eventBus;
	private final Display display;

	public RegisterPresenter(RegisterBeanServiceAsync rpcRegisterBeanService, StaffBeanServiceAsync rpcStaffBeanService, HandlerManager eventBus, Display view) {
		this.rpcStaffBeanService = rpcStaffBeanService;
		this.rpcRegisterBeanService=rpcRegisterBeanService;
		this.eventBus = eventBus;
		this.display = view;
	}
	public void bind() {
		display.getSubmitButton().addClickHandler(new ClickHandler() {   
			public void onClick(ClickEvent event) {
				register();
			}
		});
	}
	private void register() {
		registeredStaff = new RegisterBean();
		final RegisterBean newStaff=new RegisterBean();
		newStaff.setId(display.getID().getValue());
		newStaff.setId(display.getUserName().getValue());
		newStaff.setId(display.getFirstName().getValue());
		newStaff.setId(display.getLastName().getValue());
		newStaff.setId(display.getPassword().getValue());
		newStaff.setId(display.getEmail().getValue());
		newStaff.setId(display.getPnum().getValue());
		newStaff.setId(display.getSnum().getValue());
		newStaff.setId(display.getType().getValue());
		
		rpcRegisterBeanService.getStaffApprovalBean(display.getID().getValue(), new AsyncCallback<RegisterBean>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Registration cannot be sent");
			}
			@Override
			public void onSuccess(RegisterBean result) {
				if (result==null) {
					rpcStaffBeanService.getStaff(display.getID().getValue(), new AsyncCallback<StaffBean>() {
						public void onFailure(Throwable caught) {
							Window.alert("Registration cannot be sent");
						} public void onSuccess(StaffBean result) {
							if (result==null) {
								
								rpcRegisterBeanService.addNewApproval(newStaff, new AsyncCallback<Void>() {
									@Override
									public void onFailure(Throwable caught) {
										Window.alert("Registration cannot be sent");
									}
									@Override
									public void onSuccess(Void result) {
										Window.alert("Registration sent");
										eventBus.fireEvent(new AuthenticateStaff());

									}
								});
							} else {
								Window.alert("User already exists");
							}
						}
					});
				} else {
					Window.alert("Registration already exists");
				}
			}

		});
		
	}
	public void go(HasWidgets container) {
		bind();
		container.clear();
		container.add(display.asWidget());
	}

}
